package com.spring.springbootmodel.mapper;

import com.spring.springbootmodel.entyti.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Insert("insert into `user` (username,password,name,phone,email,address,avatar) " +
            "values (#{username},#{password},#{name},#{phone},#{email},#{address},#{avatar})")
    void insert(User user);

    @Update("update `user` set " +
            "username=#{username} ,name=#{name},phone=#{phone}," +
            "email=#{email},address=#{address},avatar=#{avatar} " +
            "where id=#{id}")
    void update(User user);

    @Delete("delete from user where id = #{id}")
    void deleteUserOneById(Integer id);

    @Select("select * from user order by id desc ")
    List<User> selectAll();

    @Select("select * from user where id = #{id} order by id desc")
    User selectById(Integer id);

    @Select("select * from user where name = #{name} order by id desc")
    List<User> selectByName(String name);

    @Select("select * from user where name = #{name} and username = #{username} order by id desc")
    List<User> selectByNameAndUsername(@Param("name") String name, @Param("username") String username);

    @Select("select * from user " +
            "where username like concat('%',#{username},'%') " +
            "and name like concat('%',#{name},'%') " +
            "order by id desc")
    List<User> selectByMo(String name, String username);

    @Select("select * from user " +
            "where username like concat('%',#{username},'%') " +
            "and name like concat('%',#{name},'%') " +
            "order by id desc limit #{skipNum},#{pageSize}")
    List<User> selectByLimit(@Param("name") String name,
                             @Param("username") String username,
                             @Param("skipNum") Integer skipNum,
                             @Param("pageSize") Integer pageSize);

    @Select("select count(id) from user " +
            "where username like concat('%',#{username},'%') " +
            "and name like concat('%',#{name},'%')")
    Integer selectByTotal(@Param("name") String name, @Param("username") String username);

    @Select("select * from user where username = #{username} order by id desc")
    User selectByUsername(String username);
}
